﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DomainDriver.DomainModeling.DataProviders;
using DomainDriver.DomainModeling.Repositories;
using DomainDriver.DomainModeling.GenericUnitTesting.Constraints;
using AutomotivePricingModel.TransmissionTypes;
using AutomotivePricingModel.Vehicles;

namespace AutomotivePricingModel.UnitTests.ParentChildConstraints
{
    [TestFixture(new object[] { DataSourcePersistenceType.None })]
    [TestFixture(new object[] { DataSourcePersistenceType.File })]
    [TestFixture(new object[] { DataSourcePersistenceType.Database })]
    public class TransmissionTypeVehicleParentChildConstraintTests : ParentChildConstraintTestFixture<AutomotivePricingDomainModel, TransmissionTypeEnum, Guid, TransmissionType, Vehicle>
    {
        public TransmissionTypeVehicleParentChildConstraintTests(DataSourcePersistenceType persistenceType)
            : base(persistenceType)
        {
            // do nothing
        }

        protected override AutomotivePricingDomainModel CreateDomainModel(DataSourcePersistenceType persistenceType, bool buildModelWithConstraints)
        {
            AutomotivePricingDomainModel model = new AutomotivePricingDomainModel(persistenceType, buildModelWithConstraints);
            return model;
        }

        protected override TransmissionType CreateParent(ref bool parentAlreadyAdded)
        {
            IRepository<TransmissionTypeEnum, TransmissionType> parentRepository = DomainModel.GetRepository<TransmissionTypeEnum, TransmissionType>();
            TransmissionType parent = null;

            if (parentRepository.Count() > 0)
            {
                parentAlreadyAdded = true;
                parent = parentRepository.Read().Last<TransmissionType>();
            }
            else
            {
                parentAlreadyAdded = false;
                parent = CreateFakeParent();
            }

            return parent;
        }

        protected override TransmissionType CreateFakeParent()
        {
            int numberOfEnumValues = Enum.GetValues(typeof(TransmissionTypeEnum)).Length;
            TransmissionTypeEnum fakeEnumValue = (TransmissionTypeEnum)(numberOfEnumValues + 1);

            TransmissionType parent = new TransmissionType(fakeEnumValue);
            parent.DisplayName = parent.Key.ToString();
            return parent;
        }

        protected override Vehicle CreateChild()
        {
            Vehicle child = new Vehicle();
            child.ModelYear = 2010;
            return child;
        }

        protected override void AssignParentToChild(TransmissionTypeEnum parentKey, Vehicle child)
        {
            child.OptionsConfiguration.TransmissionType = parentKey;
        }
    }
}